package com.rayboo.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.authentication.dao.ReflectionSaltSource;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

import com.rayboo.service.AuthUserDetailsService;


/**
 * 
 * @author ray
 *
 */
//@Configuration
//@EnableWebSecurity
public class WebSecurityConfigBak extends WebSecurityConfigurerAdapter {
	
	@Autowired
    private AuthUserDetailsService authUserDetailsService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        	.authorizeRequests()
        	.antMatchers("/logout").permitAll()
        	.antMatchers("/css/**","/img/**","/js/**","/common/**").permitAll()
        	.anyRequest().authenticated()
            .and()
            .formLogin()
            .loginPage("/login")
            .failureUrl("/login?error")
            .defaultSuccessUrl("/users")
            .permitAll()
            .and()
            .logout()
            .permitAll();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        ReflectionSaltSource rss = new ReflectionSaltSource();
        rss.setUserPropertyToUse("username");
        DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
        provider.setSaltSource(rss);
        provider.setUserDetailsService(authUserDetailsService);
        provider.setPasswordEncoder(new Md5PasswordEncoder());
        
        auth.authenticationProvider(provider);
        auth.userDetailsService(authUserDetailsService).passwordEncoder(new Md5PasswordEncoder());
        
        auth.inMemoryAuthentication()
        	.withUser("test")
            .password("test")
            .roles("USER");
    }
    
}